/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.panels;

import data.User;
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import maiaclient.Maia;
import maiaclient.interfaces.IUserListener;

/**
 *
 * @author groep 11
 * @date 2007-2008
 */
public class VillageInfo extends JPanel implements IUserListener {

    private JLabel lblTitle;
    private JPanel pnlTop;
    private JScrollPane treeInfoScroller;
    private String sVillageName;
    public DefaultTreeModel treemodel;
    public DefaultMutableTreeNode treeRoot;
    public JTree treeInfo;
    private Maia maia;

    public VillageInfo(Maia maia) {
        this.maia = maia;
        this.setLayout(new BorderLayout());

        lblTitle = new JLabel("Village Info");
        pnlTop = new JPanel(new BorderLayout(5, 5));
        pnlTop.setBorder(new EmptyBorder(10, 5, 0, 0));
        pnlTop.add(lblTitle, BorderLayout.NORTH);
        this.add(pnlTop, BorderLayout.NORTH);


        //RootNode 
        treeRoot = new DefaultMutableTreeNode("Please select a Village...");

        //Making the JTree 
        treemodel = new DefaultTreeModel(treeRoot);
        treeInfo = new JTree(treemodel);
        treeInfo.setSelectionModel(null);


        //Making Scrolling possible
        treeInfoScroller = new JScrollPane();
        treeInfoScroller.setViewportView(treeInfo);

        //setting Jtree
        this.add(treeInfoScroller, BorderLayout.CENTER);

        maia.addUserListener(this);

    }

    public String getSVillageName() {
        return sVillageName;
    }

    public void setSVillageName(String sVillageName) {
        this.sVillageName = sVillageName;
    }

    public void updateUser(User user) {

        treeRoot.removeAllChildren();

        treeRoot.setUserObject("Info selected Village");

        DefaultMutableTreeNode trVillage = new DefaultMutableTreeNode("Village Name: ");
        treeRoot.add(trVillage);

        DefaultMutableTreeNode trVillageName = new DefaultMutableTreeNode(getSVillageName());
        trVillage.add(trVillageName);

        DefaultMutableTreeNode tnOwner = new DefaultMutableTreeNode("Owner");
        treeRoot.add(tnOwner);

        DefaultMutableTreeNode tnParOwnerName = new DefaultMutableTreeNode("Name:");
        tnOwner.add(tnParOwnerName);

        DefaultMutableTreeNode tnOwnerName = new DefaultMutableTreeNode(user.getUsername());
        tnParOwnerName.add(tnOwnerName);

        DefaultMutableTreeNode tnParOwnerCountry = new DefaultMutableTreeNode("Country:");
        tnOwner.add(tnParOwnerCountry);

        DefaultMutableTreeNode tnOwnerCountry = new DefaultMutableTreeNode(user.getCountry());
        tnParOwnerCountry.add(tnOwnerCountry);

        treemodel.reload();


        // expanding all nodes
        for (int i = 0; i < treeInfo.getRowCount(); i++) {
            treeInfo.expandRow(i);
        }


    }
}
